/**
 * 
 */
package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import ac.svn.accesscontrol.ui.UIComponentLibrary;
import ac.svn.accesscontrol.ui.XTextPane;
import ac.svn.accesscontrol.ui.model.GroupListModel;
import ac.svn.accesscontrol.ui.model.UserListModel;

/**
 * @author Ankush
 *
 */
public class UserInfoWindow extends JFrame
{
	public interface ExportedComponents
	{
		public static final String C_ITEM_LIST_VIEW  		= "C_ITEM_LIST_VIEW";
		public static final String C_MODEL_USER_LIST 		= "C_MODEL_USER_LIST";
		public static final String C_MODEL_GROUP_LIST 		= "C_MODEL_GROUP_LIST";
		public static final String C_TEXT_PANE_DETAILS 		= "C_TEXT_PANE_DETAILS";
		public static final String C_LABEL_STATUS 				= "C_LABEL_STATUS";
		public static final String C_LABEL_LOC_ACCESS 		= "C_LABEL_LOC_ACCESS";
		public static final String C_LABEL_MEMBERSHIP 		= "C_LABEL_MEMBERSHIP";
		public static final String C_TEXT_SEARCH 		= "C_TEXT_SEARCH";
		public static final String C_LABEL_INDEX_SEARCH 	= "C_LABEL_INDEX_SEARCH";
		public static final String C_TEXT_ITEM_COUNT		= "C_LABEL_ITEM_COUNT";
	}


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel statusLabel, accessibleLocLabel, membershipLabel,indexSearchResult,lblMemCount;
	private JTextField searchField;

	private JMenuBar menuBar;
	private JList itemList;
	private UserListModel userListModel;
	private GroupListModel groupListModel;
	private JScrollPane editorScrollPane;
	private XTextPane editorPane;
	private JToolBar statusBar,toolBar;
	private JSplitPane splitPane;


	public UserInfoWindow()
	{
		super("User Information");
		//setModal(true);
		initComponents();
	}

	private void initComponents() 
	{
		setSize(800,550);
		setLayout(new BorderLayout(5,5));
		setCenteredLocation();
		setJMenuBar(getXMenuBar());
		userListModel = new UserListModel();
		groupListModel = new GroupListModel();
		itemList = new JList(userListModel);
		itemList.setFixedCellWidth(200);

		lblMemCount = new JLabel("");
		lblMemCount.setBorder(new EtchedBorder(EtchedBorder.RAISED));
		JScrollPane pane = new JScrollPane(itemList);
		pane.setBorder(new TitledBorder("Users"));
		pane.setColumnHeaderView(lblMemCount);

		getContentPane().add(pane,BorderLayout.WEST);		

		editorScrollPane = new JScrollPane();
		editorScrollPane.setBorder(new TitledBorder("Personal Information"));
		editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		editorPane = getEditorTextPane(); 
		editorPane.setEditable(false);
		pane = new JScrollPane(editorPane);
		pane.setBorder(new TitledBorder("User Access Rights"));

		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		splitPane.setTopComponent(editorScrollPane);
		splitPane.setBottomComponent(pane);
		splitPane.setBorder(new TitledBorder("User Details"));
		splitPane.setDividerLocation(0.65D);

		splitPane.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				splitPane.setDividerLocation(0.65D);		
			}
		});

		getContentPane().add(pane,BorderLayout.CENTER);


		statusLabel = new JLabel("User information manager");
		accessibleLocLabel = new JLabel("--");
		membershipLabel = new JLabel("--");
		searchField = new JTextField(10);

		statusBar = new JToolBar();
		statusBar.add(statusLabel);
		statusBar.addSeparator();
		statusBar.add(accessibleLocLabel);
		statusBar.addSeparator();
		statusBar.add(membershipLabel);
		statusBar.addSeparator();
		statusBar.setFloatable(false);

		indexSearchResult = new JLabel(" Type User/Group[F2] ");

		toolBar = new JToolBar();
		toolBar.add(indexSearchResult);
		toolBar.addSeparator();
		toolBar.add(new JLabel("  Search Text[F3]: "));
		toolBar.addSeparator();
		toolBar.add(searchField);
		toolBar.addSeparator();	

		getContentPane().add(statusBar,BorderLayout.SOUTH);
		getContentPane().add(toolBar,BorderLayout.NORTH);
		validate();

		registerComponents();
	}


	private void registerComponents() 
	{
		UIComponentLibrary.addComponent(ExportedComponents.C_ITEM_LIST_VIEW,itemList);
		UIComponentLibrary.addComponent(ExportedComponents.C_MODEL_USER_LIST, userListModel);
		UIComponentLibrary.addComponent(ExportedComponents.C_MODEL_GROUP_LIST, groupListModel);
		UIComponentLibrary.addComponent(ExportedComponents.C_TEXT_PANE_DETAILS, editorPane);
		UIComponentLibrary.addComponent(ExportedComponents.C_LABEL_STATUS,statusLabel);
		UIComponentLibrary.addComponent(ExportedComponents.C_LABEL_LOC_ACCESS,accessibleLocLabel);
		UIComponentLibrary.addComponent(ExportedComponents.C_LABEL_MEMBERSHIP,membershipLabel);
		UIComponentLibrary.addComponent(ExportedComponents.C_TEXT_SEARCH,searchField);
		UIComponentLibrary.addComponent(ExportedComponents.C_LABEL_INDEX_SEARCH,indexSearchResult);
		UIComponentLibrary.addComponent(ExportedComponents.C_TEXT_ITEM_COUNT,lblMemCount);
	}

	public XTextPane getEditorTextPane() {
		if(null == editorPane)
		{
			editorPane = new XTextPane();
		}
		return editorPane;
	}

	public static void main(String args[]) throws Exception
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		UserInfoWindow infoWindow = new UserInfoWindow();
		infoWindow.setVisible(true);
	}


	public JMenuBar getXMenuBar()
	{
		if(null == menuBar)
		{
			menuBar = new JMenuBar();
			JMenu menu = new JMenu("Action");
			JMenuItem closeMI = new JMenuItem("Exit");
			closeMI.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					setVisible(false);
				}
			});

			menu.add(closeMI);
			menuBar.add(menu);		
		}
		return menuBar;
	}

	public void setCenteredLocation()
	{
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)(size.getWidth()/2 - getWidth()/2) ,(int)(size.getHeight()/2 - getHeight()/2));
	}

	public void findText(String str)
	{
		editorPane.findText(str);
	}

}
